<?php 
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../../../TestEnv.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../../../../vendor/simpletest/unit_tester.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../../../../model/Context/BaseContext.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../../../../model/Service/EmailService.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../../../../model/Service/OrderService.php";
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."../../../../plugin/ZendFramework/library/Zend/Service/LiveDocx/MailMerge.php";
class EmailServiceTest extends UnitTestCase {
	
	function SendEmailNewOrderToCustomer(){
		try
		{	
			$orderID = 1;
			$fromEmail = 'nhilong.xipho@gmail.com';
			$toEmail = 'nhilong.xipho@yahoo.com';		
			$context = new BaseContext();
			$result = new ServiceResult();
			$emailService = new EmailService();
			$emailService->sendEmailNewOrderToCustomer($context, $result, $fromEmail, $toEmail, $orderID);
		}
		catch (Exception $e)
		{
			
		}
	}
	
	function testabc(){
		try
		{	
			$toEmail = 'nhilong.xipho@gmail.com';
			$title = 'test mail';
			$html = 'noi dung test mail';
			$text = 'noi dung test mail';
			$fromMail = 'nhilong.xipho@yahoo.com';
			$fromName = 'mjnthj';
			$filename = 'document4.pdf';
			$file = dirname(__FILE__).DIRECTORY_SEPARATOR.'document4.pdf';
		    $file_size = filesize($file);
		    $handle = fopen($file, "r");
		    $content = fread($handle, $file_size);
		    fclose($handle);
		    $content = chunk_split(base64_encode($content));
			$header = "From: ".$fromName." <".$fromMail.">\n";
			$header .= "Reply-To: ".$fromName." <".$fromMail.">\n";
			$header .= "X-Mailer: ".$fromName."\n";
	
	
			$boundary = "Boundary" .md5(time()). "1";
			$boundary2 = "Boundary2" .md5(time()). "2";
			$boundary3 = "Boundary3" .md5(time()). "3";
			$boundary4 = "Boundary4" .md5(time()). "4";
			$boundary5 = "Boundary5" .md5(time()). "5";
	
			
			$header .= "MIME-Version: 1.0\n";
			$header .= "Content-Type: multipart/related;\n";
			$header .= " type=\"multipart/alternative\";\n";
			$header .= " boundary=\"$boundary\";";
	
	
			$message = "--$boundary\n";
			$message .= "Content-Type: multipart/alternative;\n";
			$message .= " boundary=\"$boundary2\";\n\n";
	
	
			$message .= "--$boundary2\n";
			$message .= "Content-Type: text/plain; charset=utf-8\n";
			$message .= "Content-Transfer-Encoding: 7bit\n\n";
	
			$message .= "$text\n\n";
	
			$message .= "--$boundary2\n";
			$message .= "Content-Type: text/html; charset=utf-8\n";
			$message .= "Content-Transfer-Encoding: 7bit\n\n";
			
			$message .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
	<html>
	<head>
	<style>
	body {
		text-align: center;
		font-family: arial, sans-serif;
	        font-size: 10px;
	        color: #c9c9c9;
		width: 100%;
		margin: 0;
		padding: 0;
	}
	#content {
		width: 65%;
		text-align: left;
		margin: 5% auto;
	}
	#header {
		margin: 0 0 15px 0;
	}
	.title {
	  background-color: #333333;
	  border-bottom: 4px solid #000000;
	  color: #fff;
	  padding: 7px;
	  font-size: 16px;
	  font-weight: bold;
	}
	.invite {
	  font-size: 12px;
	}
	.space{
		height: 0px;
	}
	.body {
	  border: 1px solid #e1e1e1;
	  padding: 28px;
	  font-size: 12px;
	  color: #262626;
	  margin: 0 0 15px 0;
	}
	.b {
	  color: #666;
	  font-style: italic;
	}
	</style>
	</head>
	<body>
	<div id=\"content\">
	<div id=\"header\">
	<table width=\"100%\" cellspacing=\"0\">
	    <tr>
	      <td height=\"28\" style=\"background-color: #333333; border-bottom: 4px solid #000000;\" class=\"title\"><font size=\"3\" color=\"#FFFFFF\">&nbsp;&nbsp;<b>$title</b></font></td>
	    </tr>
	    <tr>
	      <td style=\"text-align: left; border: 1px solid #e1e1e1; padding: 28px; font-size: 12px; color: #262626; margin: 0 0 15px 0\" class=\"body\">
	      $html
	      </td>
	    </tr>
	  </table><br/>
	  <span class=\"b\">&#167; Legal notes:</span> We may access and/or disclose your personal information if we believe such action is necessary to: (a) comply with the law or legal process served on BArt-System; (b) protect and defend the rights or property of BArt-System (including the enforcement of our agreements); or (c) act in urgent circumstances to protect the personal safety of users of BArt-System websites or members of the public.
	</div>
	</body>
	</html>\n";
	      
	      
	
	       
	      
		  
		  $message .= "--$boundary\n";
			$message .= "Content-Type: multipart/mixed; boundary=\"".$boundary3."\"\r\n\r\n";
		    //$header .= "This is a multi-part message in MIME format.\r\n";
		    $message .= "--".$boundary3."\r\n";
		    
		    //$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
		    //$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
		    //$header .= $message."\r\n\r\n";
		    //$header .= "--".$boundary."\r\n";
		    
		    $message .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
		    $message .= "Content-Transfer-Encoding: base64\r\n";
		    $message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
		    $message .= $content."\r\n\r\n";
		    //$header .= "--".$boundary2."--";
		    
		    $message .= "\n--$boundary3--\n";
	      $message .= "\n--$boundary2--\n";
	      $message .= "\n--$boundary--\n";
	    //  $message .= "Content-type: application/msword; name=$filename\r\n";
		//$message .= "Content-transfer-encoding:base64\r\n\r\n";
		//$message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
		//$message .= $content. "\r\n\r\n";
		//$message .= "--".$boundary2."--";
		
	      //$this->mail_utf8($toEmail,$title,$message,$header);
	      
	      $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
	      //mail($toEmail,'=?UTF-8?B?'.base64_encode($title).'?=',$message,$header);
      
      
      
      
      
      
			
			$fromEmail = 'hoangmy@yahoo.com';
			$toUname = 'khanhthanh';		
			$context = new BaseContext();
			$result = new ServiceResult();
			$orderService = new OrdersService();
			$invoiceService = new InvoiceService();
			//$order = $orderService->getOrderByOid($context, $result, 151);
			//$emailService = new EmailService();
			//$emailService->sendEmailForgotPasswordNotification($context, $result, $fromEmail, $toUname);
			//$orderFactArr = $orderService->getPurchaseOrderArr($context, $result, 161);
			$invoice = $invoiceService->getInvoiceByInvoiceID($context, $result, 22);
			$reportService = new ReportService();
			$reportService->setSiteURL('http://localhost/bart-system');
			//echo "<pre>";
			//print_r($orderFactArr);
			$i = 1;
			echo strlen("812001A");
			//foreach ($orderFactArr as $order)
			//{
				//echo $order->getOrdersDomain()->getConsignee();
				//if($order->getOrdersDomain()->getOrderID())
				//{
					$isOrder = false;
				//}
				if($i == 3)
				{
					//break;
				}
				//$mailMerge = new Zend_Service_LiveDocx_MailMerge();
				//$mailMerge->setUsername("nhilong");
				//$mailMerge->setPassword("stunninST@#");
				//$mailMerge->setLocalTemplate(dirname(__FILE__).DIRECTORY_SEPARATOR.'order.doc');
				//$billConnections = array (
				//    array ('code' => '51105', 'art_desc' => 'Amoa Eetkamertafel 160 rechte poot ', 'line' => 'Acacia Sandblasted', 'color' => 'White', 'width' => '160', 'height' => '78', 'depth' => '34', 'cbm' => '7,20', 'qly' => '20', 'rate' => '107,69', 'cur' => 'USD', 'amount' => '2.153,80'),
				//    array ('code' => '42344', 'art_desc' => 'Amoa Eetkamertafel 423 rechte poot ', 'line' => 'Acacia Sandblasted', 'color' => 'Red', 'width' => '343', 'height' => '54', 'depth' => '56', 'cbm' => '7,20', 'qly' => '56', 'rate' => '107,69', 'cur' => 'USD', 'amount' => '1.342,80'),
				//    array ('code' => '65775', 'art_desc' => 'Amoa Eetkamertafel 656 rechte poot ', 'line' => 'Acacia Sandblasted', 'color' => 'Yellow', 'width' => '67', 'height' => '76', 'depth' => '65', 'cbm' => '7,20', 'qly' => '45', 'rate' => '107,69', 'cur' => 'USD', 'amount' => '4.34,80'),
				//   array ('code' => '45345', 'art_desc' => 'Amoa Eetkamertafel 788 rechte poot ', 'line' => 'Acacia Sandblasted', 'color' => 'Gray', 'width' => '435', 'height' => '12', 'depth' => '45', 'cbm' => '7,20', 'qly' => '45', 'rate' => '107,69', 'cur' => 'USD', 'amount' => '3.342,80'),
				//);
				
				//$rs = $reportService->generatePDFFile($context, $result, $order);
				$rs = $reportService->generateInvoicePDFFile($context, $result, $invoice);
				echo "ket qua gen: ".$rs;
				echo "kqua".$i;
				echo "<br />";
				$i++;
			//}
			echo "finish";
			/*$billConnections = array (
			    array ('connection_numbera' => '            +49 421 335 912      ', 'connection_duration' => '00:00:07', 'fee' => '0.03'),
			    array ('connection_numbera' => '            +49 421 335 913      ', 'connection_duration' => '00:00:07', 'fee' => '0.03'),
			    array ('connection_numbera' => '            +49 421 335 914      ', 'connection_duration' => '00:00:07', 'fee' => '0.03'),
			    array ('connection_numbera' => '            +49 421 335 916      ', 'connection_duration' => '00:00:07', 'fee' => '0.03')
			);*/
			//print_r($billConnections);
			//$date = date('F d,Y');
			//echo $date;
			//$mailMerge->assign('article', $billConnections);
			//$mailMerge->assign('article1', $billConnections);
			//$mailMerge->assign('ga1', $billConnections);
			//$mailMerge->assign('code', 234324234);
			//$mailMerge->assign('art_desc', 'description');
			//$mailMerge->assign('line', 'sdfa');
			//$mailMerge->assign('color', 'Red');
			//$mailMerge->assign('cur', 'USD');
			//$mailMerge->createDocument();
			//$document = $mailMerge->retrieveDocument('pdf');

			//unset($mailMerge);
			
			//file_put_contents('document4.pdf', $document);
		}
		catch (Exception $e)
		{
			
		}
	}
	
	
}
?>